package com.company.bookstore.controller.public_module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.bookstore.common.redis.RedisUtil;
import com.company.bookstore.common.result.MvcResult;
import com.company.bookstore.entities.book_module.Book;
import com.company.bookstore.entities.user_admin_module.Schedule;
import com.company.bookstore.service.book_module.BookService;
import com.company.bookstore.service.user_admin_module.ScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-22
 */
@RestController
@Slf4j
@RequestMapping("/public/book")
public class PublicBookController {

    @Resource
    private BookService bookService;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ScheduleService scheduleService;

//    @GetMapping("/getSecondKill")
//    public MvcResult<Object> getSecondKillBook() {
//        return MvcResult.builder().code(200).message("获取秒杀图书列表成功").object(redisUtil.getSecondKillBookMap()).build();
//    }
    @GetMapping("")
    public MvcResult<Object> getSchedule(){
        List<Schedule> schedules = scheduleService.list(new QueryWrapper<Schedule>().in("status",1));
        schedules.forEach(
                v->{
                    Book book = bookService.getBookById(v.getFkBookId());
                    v.setBook(book);
                    v.setKillPrice(Math.floor(book.getPrice()*Math.random()));
                }
        );
        return MvcResult.builder().code(200).object(schedules).message("成功获取秒杀活动图书").build();
    }

    @GetMapping("/getName")
    public MvcResult<Object> getName(){
        return MvcResult.builder().code(200).message("获取图书列表成功").object(bookService.getNames()).build();
    }



    @GetMapping("/getBookById/{bookId}")
    public MvcResult<Object> getBookById(@PathVariable Integer bookId) {
        Book book = bookService.getBookById(bookId);
        if (book == null) {
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(book);
            return MvcResult.SELECT_SUCCESS;
        }
    }

    /**
     * @param current   当前页面
     * @param size      总共页面
     * @param orderType =0综合排序（随机排序） 1综合排序 2销量降序 3价格从低到高 4价格从高到低
     * @param from      价格起点  (需要判断是否有值，有值则在区间内排序，无值则全部排序）
     * @param to        价格上限
     * @return 返回MvcResult封装的结果集
     */
    @GetMapping("/getList/{current}/{size}/{orderType}")
    public MvcResult<Object> getAllBooks(@PathVariable Integer current, @PathVariable Integer size, @PathVariable Integer orderType,
                                         @RequestParam(required = false) Integer from, @RequestParam(required = false) Integer to) {
        IPage<Book> books = bookService.getAllBooks(new Page<>(current, size), orderType, from, to); //加参数
        if (books.getRecords().size() == 0) {
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(books);
            return MvcResult.SELECT_SUCCESS;
        }
    }

    @GetMapping("/no/getList/{current}/{size}/{orderType}")
    public MvcResult<Object> getAllBooksForFront(@PathVariable Integer current, @PathVariable Integer size, @PathVariable Integer orderType,
                                                 @RequestParam(required = false) Integer from, @RequestParam(required = false) Integer to) {

        IPage<Book> books = bookService.getAllBooksForFront(new Page<>(current, size), orderType, from, to); //加参数
        if ((from != null || to != null) && books.getRecords().size() == 0) {
            MvcResult.SELECT_FAIL.setMessage("没找到该价格区间的图书");
            return MvcResult.SELECT_FAIL;
        } else if ((from == null && to == null) && books.getRecords().size() == 0) {
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(books);
            return MvcResult.SELECT_SUCCESS;
        }
    }

    /**
     * @param current   同上
     * @param size      同上
     * @param cId       图书种类ID
     * @param orderType =0综合排序（随机排序） 1综合排序 2销量降序 3价格从低到高 4价格从高到低
     * @param from      同上
     * @param to        同上
     * @return 同上
     */
    @GetMapping("/getBooksByCid/{current}/{size}/{cId}/{orderType}")
    public MvcResult<Object> getBooksByCid(@PathVariable Integer current, @PathVariable Integer size,
                                           @PathVariable Integer cId, @PathVariable Integer orderType,
                                           @RequestParam(required = false) Integer from, @RequestParam(required = false) Integer to) {
        IPage<Book> books = bookService.getBooksByCid(new Page<>(current, size), cId, orderType, from, to);
        if (books.getRecords().size() == 0) {
            MvcResult.SELECT_FAIL.setMessage("暂无该种类的书籍");
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(books);
            return MvcResult.SELECT_SUCCESS;
        }
    }

    @GetMapping("/no/getBooksByCid/{current}/{size}/{cId}/{orderType}")
    public MvcResult<Object> getBooksByCidForFront(@PathVariable Integer current, @PathVariable Integer size,
                                                   @PathVariable Integer cId, @PathVariable Integer orderType,
                                                   @RequestParam(required = false) Integer from, @RequestParam(required = false) Integer to) {
        IPage<Book> books = bookService.getBooksByCidForFront(new Page<>(current, size), cId, orderType, from, to);
        if ((from != null || to != null) && books.getRecords().size() == 0) {
            MvcResult.SELECT_FAIL.setMessage("没找到该价格区间的图书");
            return MvcResult.SELECT_FAIL;
        } else if ((from == null && to == null) && books.getRecords().size() == 0) {
            MvcResult.SELECT_FAIL.setMessage("暂无该种类的书籍");
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(books);
            return MvcResult.SELECT_SUCCESS;
        }
    }


    /**
     * @param current   同上
     * @param size      同上
     * @param str       搜索内容字符串
     * @param type      1.根据书名 2.根据作者 3.根据分类
     * @param orderType =1综合排序 2销量降序 3价格从低到高 4价格从高到低
     * @param from      同上
     * @param to        同上
     * @return 同上
     */
    @GetMapping("/search/{current}/{size}/{type}/{orderType}")
    public MvcResult<Object> search(@PathVariable Integer current, @PathVariable Integer size,
                                    @PathVariable int type, @PathVariable Integer orderType, String str,
                                    @RequestParam(required = false) Integer from, @RequestParam(required = false) Integer to) {
        IPage books = bookService.searchBooks(new Page<>(current, size), type, orderType, str, from, to);
        if (books.getRecords().size() == 0) {
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(books);
            return MvcResult.SELECT_SUCCESS;
        }
    }

    @GetMapping("/no/search/{current}/{size}/{type}/{orderType}")
    public MvcResult<Object> searchForFront(@PathVariable Integer current, @PathVariable Integer size,
                                            @PathVariable int type, @PathVariable Integer orderType, String str,
                                            @RequestParam(required = false) Integer from, @RequestParam(required = false) Integer to) {
        IPage books = bookService.searchBooksForFront(new Page<>(current, size), type, orderType, str, from, to);
        if (books.getRecords().size() == 0) {
            MvcResult.SELECT_FAIL.setMessage("抱歉！没有找到与 " + str + " 相关的内容！");
            return MvcResult.SELECT_FAIL;
        } else {
            MvcResult.SELECT_SUCCESS.setObject(books);
            return MvcResult.SELECT_SUCCESS;
        }
    }


}
